﻿@page "/demo/counter"
@inherits BaseComponent
@attribute [ReuseTabsPage(Title = "计数器")]
@attribute [Menu(AppConstant.Demo, "计数器", "plus-circle", 2)]

<PageTitle>Counter</PageTitle>

<div class="kui-card" style="padding:20px">
    <h1>Counter</h1>

    <p role="status">Current count: @currentCount</p>

    <KButton Type="@ButtonType.Primary" Name="Click me" OnClick="IncrementCount" />
    <KButton Type="@ButtonType.Primary" Name="耗时" OnClick="OnLoad" />

    <AntForm Form="model">
        <AntRow>
            <DataItem Span="8" Label="附件">
                <KUpload MultiFile Accept=".jpg, .png, .doc" />
            </DataItem>
            <DataItem Span="8" Label="用户">
                <UserPicker Value="@context.User" Text="@context.User" ValueChanged="value=>context.User=value?.ToString()" />
            </DataItem>
        </AntRow>
        <AntRow>
            <DataItem Span="8" Label="测试">
                <AntCheckboxGroup @bind-Value="context.Fields" Category="Item1,Item2,Item3" />
            </DataItem>
            <DataItem Span="8" Label="测试">
                <AntSelectCode @bind-Value="context.Field" Category="Item1,Item2,Item3" />
            </DataItem>
        </AntRow>
        <div>@string.Join(",", value)</div>
        <div>@model.Data.Field</div>
    </AntForm>
</div>

@code {
    private int currentCount = 0;
    private string[] value = [];
    private FormModel<Test> model;
    private Test test = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        model = new FormModel<Test>(this);
        model.Data = new Test();
    }

    private void IncrementCount()
    {
        currentCount++;
    }

    private void OnLoad()
    {
        App?.ShowSpinAsync("耗时操作中...", () =>
        {
            Thread.Sleep(10000);
            return Task.CompletedTask;
        });
    }

    class Test
    {
        public string Field { get; set; }
        public string User { get; set; }

        public string[] Fields
        {
            get { return Field?.Split(','); }
            set { Field = string.Join(',', value); }
        }
    }
}
